function [data2] = loadDataFiles(dataId)
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	%%%%% Inputs:
	% dataId:				integer
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	%%%%% Outputs:
	% data2:				object
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

if dataId == 1
	timeAggLevel = 'week';
	geoAggLevel =  'CantonId';
	datasetName = 'CantonId_week';
	datasetFile          = sprintf('../../data/data_mat/covariates/covariates_%s.mat', datasetName);
	inputConsumerAdoptionsFile = '../../data/data_mat/platformData/consumerAdoptionsData_CantonId_week.mat';
	inputProviderMovementsFile = '../../data/data_mat/platformData/providerMovementsData_CantonId_week.mat';
end
if dataId == 2
	timeAggLevel = 'day';
	geoAggLevel =  'CantonId';
	datasetName = 'CantonId_day';
	datasetFile          = sprintf('../../data/data_mat/covariates/covariates_%s.mat', datasetName);
	inputConsumerAdoptionsFile = '../../data/data_mat/platformData/consumerAdoptionsData_CantonId_day.mat';
	inputProviderMovementsFile = '../../data/data_mat/platformData/providerMovementsData_CantonId_day.mat';
end
if dataId == 3
	data2 = loadDataFilesCtsTime(dataId);
	return;
end

if strcmp(timeAggLevel, 'week'); tt_subset_idxes = 1:68;  end;
if strcmp(timeAggLevel, 'day');  tt_subset_idxes = 1:476; end;

locationAggregationsFolder = sprintf('../../data/data_mat/timeLocationAggregations/location/%s', geoAggLevel);
timeAggregationsFolder     = sprintf('../../data/data_mat/timeLocationAggregations/time/%s',     timeAggLevel);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%% LOAD CONSUMER ADOPTION DATA %%%%%%%%%%
load(inputConsumerAdoptionsFile, 'adoptionData');
	% adoptionData:    object
	%     .pastConsumerTransactionsOrig: K_consumer x 1
	%     .pastConsumerTransactionsDest: K_provider x 1
	%     .transactions:           Tall x K_provider x K_consumer
	%     .geoLabels:              cell(Kall,1) of strings
	%     .timeLabels:             cell(T,1) of strings

%%% Subset time
adoptionData.transactions = adoptionData.transactions(tt_subset_idxes,:,:); % T x K_provider x K_consumer
adoptionData.timeLabels   = adoptionData.timeLabels(tt_subset_idxes); % cell(T,1) of strings

%%% Make NewConsumers
adoptionData.transactions = permute(adoptionData.transactions, [1 3 2]); % T x K_consumer x K_provider
NewConsumers = adoptionData.transactions; % T x K_consumer x K_provider

% Make initLaggedConsumersOrig from pastConsumerTransactions
if tt_subset_idxes(1) ~= 1; error('The code here assumes pastConsumerTransactions are transactions before tt=1. Some fix needed in case first tt_idx ~= 1'); end;
initLaggedConsumersOrig = adoptionData.pastConsumerTransactionsOrig; % K_consumer x 1
initLaggedConsumersDest = adoptionData.pastConsumerTransactionsDest; % K_provider  x 1


%%%%%%%%%% LOAD PROVIDER MOVEMENT DATA %%%%%%%%%%
load(inputProviderMovementsFile, 'movementData');
movementData.values = movementData.values(tt_subset_idxes,:,:); % T x K x NumVars
NewProviders     = movementData.values(:,:,find(strcmp(movementData.VarNames, 'NewProviders'))); % T x K
DeletedProviders = movementData.values(:,:,find(strcmp(movementData.VarNames, 'DeletedProviders'))); % T x K
laggedProviders  = movementData.values(:,:,find(strcmp(movementData.VarNames, 'LaggedProviders'))); % T x K


%%%%%%%%%% LOAD DAILY/WEEKLY DATA with covariates %%%%%%%%%
config.datasetName                = datasetName;
config.datasetFile                = datasetFile;
config.timeAggLevel               = timeAggLevel;
config.geoAggLevel                = geoAggLevel;
config.surfaceVar_Zname           = 'Area';
config.laggedAdoptionEffects      = {{}}; % (ignored any way...)
config.locationAggregationsFolder = locationAggregationsFolder;
config.timeAggregationsFolder     = timeAggregationsFolder;
[data, surfaces, surfaceName, population, geoAggLevelNames, geoAggLevelLabels, ...
        timeAggLevelNames, timeAggLevelLabels] = getRealData0(config);

%%% Subset time
data.X = data.X(tt_subset_idxes,:,:); % T x K x NumX
data.A = data.A(tt_subset_idxes,:); % T x NumA
data.timeLabels = data.timeLabels(tt_subset_idxes); % cell(1,T) of strings

%% Get periodLabels and geoLabels
periodLabels = data.timeLabels;
geoLabels0    = data.geoLabels;


if ~isequal(adoptionData.geoLabels, geoLabels0); error('geoLabels do not match!'); end;
if ~isequal(movementData.geoLabels, geoLabels0); error('geoLabels do not match!'); end;

% Clean up
clear adoptionData movementData;


%% Get incomingCommuters and touristBeds (useful in counterfactuals)
incomingCommuters = data.Z(:,find(strcmp(data.Znames, 'NumCommutersToLocation'))); % K x 1
touristBeds       = data.Z(:,find(strcmp(data.Znames, 'NumTouristBeds'))); % K x 1


%%%%% Remove adoptions of consumers who appear to rent a car in a location with no active provider
%%%%% Make NewConsumers and laggedConsumersOrig
[T,K1,K2] = size(NewConsumers);

%%% Process observations where laggedProviders = 0:
% Find corresponding tks2 that should be "deleted"
is_bad_tk2  = laggedProviders <= 0; % T x K2

% Set the corresponding NewConsumers's to zero
%disp(sprintf('TOTAL NUMBER OF CONSUMER ADOPTIONS BEFORE REMOVING WEIRD ONES: %d', sum(NewConsumers(:))));
NewConsumers = reshape(NewConsumers, [T K1 K2]);
for k2 = 1:K2
	bad_tt = find(is_bad_tk2(:,k2));
	tmp = NewConsumers(bad_tt, :, k2);
%	if any(tmp(:)) > 0
%		disp(sprintf('Enforcing NewConsumers=0 because no car active in location %d -- number of transactions deleted: %d', k2, sum(tmp(:))));
%	end
	NewConsumers(bad_tt, :, k2) = 0;
end

%%%%% Compute laggedConsumersOrig and laggedConsumersDest
	% laggedConsumersOrig_{tk1} is sum_{t'} < t sum_{k2} Y_{t'k1k2} : number of consumers living in k1 who have had their first-time rental
	% laggedConsumersDest_{tk2} is sum_{t'} < t sum_{k1} Y_{t'k1k2} : number of consumers who have had their first-time rental in k2
consumerAdoptionsOrig = reshape(sum(reshape(NewConsumers, [T*K1 K2]), 2), [T K1]); % T x K1
lagged_cumsum_consumerAdoptionsOrig = cumsum(consumerAdoptionsOrig,1) - consumerAdoptionsOrig; % T x K1
laggedConsumersOrig = initLaggedConsumersOrig' + lagged_cumsum_consumerAdoptionsOrig; % T x K1
clear consumerAdoptionsOrig;

consumerAdoptionsDest = reshape(sum(NewConsumers,2), [T K2]); % T x K2
lagged_cumsum_consumerAdoptionsDest = cumsum(consumerAdoptionsDest,1) - consumerAdoptionsDest; % T x K2
laggedConsumersDest = initLaggedConsumersDest' + lagged_cumsum_consumerAdoptionsDest; % T x K2
clear consumerAdoptionsDest;

% Reshape NewConsumers and make it sparse
NewConsumers = reshape(NewConsumers, [T*K1*K2 1]); % (T*K1*K2) x 1
NewConsumers = sparse(NewConsumers);               % (T*K1*K2) x 1 (sparse)

% Store everything into object data2 to be output
data2.NewConsumers        = NewConsumers;        % (T*K1*K2) x 1 (sparse)
data2.NewProviders         = NewProviders;         % T x K
data2.DeletedProviders     = DeletedProviders;     % T x K
data2.laggedConsumersOrig = laggedConsumersOrig; % T x K1
data2.laggedConsumersDest = laggedConsumersDest; % T x K2
data2.laggedProviders      = laggedProviders;      % T x K
data2.population        = population;        % K x 1
data2.surfaces          = surfaces;          % K x 1
data2.incomingCommuters = incomingCommuters; % K x 1
data2.touristBeds       = touristBeds;       % K x 1
data2.periodLabels = periodLabels;
data2.geoLabels    = geoLabels0;

data2.A     = data.A;
data2.Z     = data.Z;
data2.X     = data.X;
data2.Anames     = data.Anames;
data2.Znames     = data.Znames;
data2.Xnames     = data.Xnames;

data2.surfaceName = config.surfaceVar_Zname;
data2.geoAggLevelNames    = geoAggLevelNames;
data2.geoAggLevelLabels   = geoAggLevelLabels;
data2.timeAggLevelNames   = timeAggLevelNames;
data2.timeAggLevelLabels = timeAggLevelLabels;

end
